Passed
Push — dev ( aba162...11ad76 )
by Kasper
45s queued 12s
created

scooter_hive.js ➔ startUpdate   A

Complexity

Conditions 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
/**
2
 * A node application to start a X amount of scooters.
3
 * Used to simulate a system of X scooters.
4
 * scooter.js will load .env file
5
 * This will later be removed, since docker will handle loading environment variables
6
 * Environment variables:
7
 * DBURI
8
 * NUMBER_OF_SCOOTERS
9
 */
10
11
const { Scooter } = require("./scooter");
12
const db = require("./modules/sparkdb");
13
14
const numberOfScooters = process.env.NUMBER_OF_SCOOTERS;
15
let scooters = [];
16
17
/**
18
  * Loads x number of scooters and returns them
19
  * @return list of scooter
20
  */
21
async function loadNewScooters() {
22
    const scooterArray = [];
23
    for (let i = 0; i < numberOfScooters; i++) {
24
        const scooter = new Scooter(removeScooter);
25
        await scooter.load(); // Load up new scooter, can pass id if custom generation is wanted
26
        scooterArray.push(scooter);
27
    }
28
    return scooterArray;
29
}
30
31
32
/**
33
  * @param mixed scooterArray
34
  * 
35
  * @return [type]
36
  */
37
async function startUpdateScooters() {
38
    scooters.forEach(scooter => {
39
        scooter.update();
40
    });
41
}
42
43
/**
44
 * Prints useful information about the current state of the scooters
45
  * @param mixed scooters
46
  * @param bool repeat
47
  * 
48
  * @return [type]
49
  */
50
async function printScooters(repeat) {
51
    const totalScooters = scooters.length;
52
    const scootersStateCount = {};
53
    for (let i = 0; i < totalScooters; i++) {
54
        const scooter = scooters[i];
55
        // console.log(scooter);
56
        scootersStateCount[scooter.status] = scootersStateCount[scooter.status] ? scootersStateCount[scooter.status] + 1 : 1;
57
    }
58
    for (const state in scootersStateCount) {
59
        console.log(state, scootersStateCount[state]);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
60
    }
61
    console.log("============================");
62
    if (repeat) {
63
        setTimeout(() => printScooters(repeat), 5000);
64
    }
65
}
66
67
/**
68
 * @param string id
69
 * 
70
 * @return void
71
 */
72
function removeScooter(id) {
73
    let index = -1;
74
    for (let i = 0; i < scooters.length; i++) {
75
        if (id === scooters[i]._id.toString()) {
76
            index = i;
77
            break;
78
        }
79
    }
80
    if (index !== -1) {
81
        console.log(`${scooters[index].name}: removed from database`);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
82
        scooters.splice(index, 1);
83
    }
84
}
85
86
/**
87
  * @return [type]
88
  */
89
async function dropCallback() {
90
    console.log("Loading up new scooters:", numberOfScooters);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
91
    const existingScooters = await db.getAllScooters();
92
    const oldScooters = [];
93
    for (let i = 0; i < existingScooters.length; i++) {
94
        const scooter = new Scooter(removeScooter);
95
        await scooter.load(existingScooters[i]._id.toString());
96
        oldScooters.push(scooter);
97
    }
98
    scooters = await loadNewScooters();
99
    scooters = scooters.concat(oldScooters);
100
    console.log("Starting scooters")
101
    startUpdateScooters();
102
    printScooters(true);
103
    //TODO: Add watch on mongoDB database
104
    //IF NEW SCOOTER, ADD IT
105
    //IF SCOOTER IS REMOVED, REMOVES IT FROM ARRAY
106
}
107
108
async function main() {
109
    db.setMongoURI(process.env.DBURI);
110
    db.connect();
111
    // console.log("Dropping scooter collection...");
112
    // db.dropScooters(dropCallback);
113
    dropCallback();
114
}
115
116
if (require.main === module) {
117
    main();
118
}
119